package com.qczl.pme.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qczl.pme.dao.PersonDao;
import com.qczl.pme.dao.TopicDao;
import com.qczl.pme.dao.TopicPersonDao;
import com.qczl.pme.entity.PMEPerson;
import com.qczl.pme.service.PersonService;
import com.qczl.pme.vo.PersonVo;

/**
 * 人员信息业务逻辑层实现类
 * 
 * @作者:SongR
 * @日期:2017年9月19日下午4:34:39
 */
@Service
public class PersonServiceImpl implements PersonService {

	@Autowired
	TopicPersonDao tpdao;

	@Autowired
	TopicDao topicdao;

	@Autowired
	PersonDao persondao;

	/**
	 * 实现获取需要签到人员信息
	 */
	@Override
	public List<PersonVo> getMettingPersonV1(String stbno, String topicid, String start, String size) throws Exception {
		String hql = "select a from PMEPerson a,PMETopicPerson b where a.id=b.personid and b.topicid=" + topicid
				+ " and a.status=0 order by a.sort";
		List<PMEPerson> personArray = new ArrayList<PMEPerson>();
		if (size == "all") {
			personArray = persondao.find(hql);
		} else {
			persondao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		}
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		for (PMEPerson p : personArray) {
			PersonVo vo = new PersonVo();
			BeanUtils.copyProperties(p, vo);
			vo.setImg("/resource/person/" + p.getIdCard() + ".png");
			voArray.add(vo);
		}
		return voArray;
	}

	/**
	 * 实现获取需要签到人员数
	 */
	@Override
	public Long countMettingPersonV1(String stbno, String topicid) throws Exception {
		String hql = "select count(*) from PMEPerson a,PMETopicPerson b where a.id=b.personid and b.topicid=" + topicid
				+ " and a.status=0";
		return persondao.count(hql);
	}

	/**
	 * 实现获取签到人员
	 */
	@Override
	public List<PersonVo> getSignPersonV1(String stbno, String mettingid, String start, String size) throws Exception {
		String hql = "select a from PMEPerson a,PMESign b where a.id=b.personid and b.mettingid='" + mettingid
				+ "' and b.signtypeid=1 and b.state=0  order by b.signtime";
		List<PMEPerson> personArray = new ArrayList<PMEPerson>();
		if (size == "all") {
			personArray = persondao.find(hql);
		} else {
			persondao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		}
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		for (PMEPerson p : personArray) {
			PersonVo vo = new PersonVo();
			BeanUtils.copyProperties(p, vo);
			vo.setImg("/resource/person/" + p.getIdCard() + ".png");
			voArray.add(vo);
		}
		return voArray;
	}

	/**
	 * 实现获取签到人员数
	 */
	@Override
	public Long countSignPersonV1(String stbno, String mettingid) throws Exception {
		String hql = "select count(*) from PMEPerson a,PMESign b where a.id=b.personid and b.mettingid='" + mettingid
				+ "'  and b.signtypeid=1 and b.state=0 and b.stbno='" + stbno + "'";
		return persondao.count(hql);
	}

	/**
	 * 实现获取坐班签到人员
	 */
	@Override
	public List<PersonVo> getZuobanPersonV1(String stbno, int start, int size) throws Exception {
		String hql = "select a from PMEPerson a,PMESign b where a.id=b.personid and b.signtypeid=2 and b.stbno='"
				+ stbno + "' and b.state=0 and a.status=0 order by b.signtime desc";
		List<PMEPerson> personArray = persondao.findP(hql, start, size);
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		for (PMEPerson p : personArray) {
			PersonVo vo = new PersonVo();
			BeanUtils.copyProperties(p, vo);
			vo.setImg("/resource/person/" + p.getIdCard() + ".png");
			voArray.add(vo);
		}
		return voArray;
	}

	/**
	 * 实现获取坐班签到人员数
	 */
	@Override
	public Long countZuobanPersonV1(String stbno) throws Exception {
		String hql = "select count(*) from PMEPerson a,PMESign b where a.id=b.personid and b.signtypeid=2 and b.stbno='"
				+ stbno + "' and b.state=0 and a.status=0";
		return persondao.count(hql);
	}

	/**
	 * 实现获取活动签到人员
	 */
	@Override
	public List<PersonVo> getHuodaongPersonV1(String stbno, int start, int size) throws Exception {
		String hql = "select a from PMEPerson a,PMESign b where a.id=b.personid and b.signtypeid=3 and b.stbno='"
				+ stbno + "' and b.state=0 and a.status=0 order by b.signtime desc";
		List<PMEPerson> personArray = persondao.findP(hql, start, size);
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		for (PMEPerson p : personArray) {
			PersonVo vo = new PersonVo();
			BeanUtils.copyProperties(p, vo);
			vo.setImg("/resource/person/" + p.getIdCard() + ".png");
			voArray.add(vo);
		}
		return voArray;
	}

	/**
	 * 实现获取活动签到人员数
	 */
	@Override
	public Long countHuodongPersonV1(String stbno) throws Exception {
		String hql = "select count(*) from PMEPerson a,PMESign b where a.id=b.personid and b.signtypeid=3 and b.stbno='"
				+ stbno + "' and b.state=0 and a.status=0";
		return persondao.count(hql);
	}

	/**
	 * 实现获取会议签到人员
	 */
	@Override
	public List<PersonVo> getHuiyiPersonV1(String stbno, int start, int size) throws Exception {
		String hql = "select a from PMEPerson a,PMESTB b where a.status=0 and b.status=0 and a.area.id=b.area.id and b.no='"
				+ stbno + "' order by a.sort";
		List<PMEPerson> personArray = persondao.findP(hql, start, size);
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		for (PMEPerson p : personArray) {
			PersonVo vo = new PersonVo();
			BeanUtils.copyProperties(p, vo);
			vo.setImg("/resource/person/" + p.getIdCard() + ".png");
			voArray.add(vo);
		}
		return voArray;
	}

	/**
	 * 实现获取会议签到人员数
	 */
	@Override
	public Long countHuiyiPersonV1(String stbno) throws Exception {
		String hql = "select count(*) from PMEPerson a,PMESTB b where a.status=0 and b.status=0 and a.area.id=b.area.id and b.no='"
				+ stbno + "'";
		return persondao.count(hql);
	}

}
